package com.wtx.atguigu.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @description:
 * @author: wtx
 * @createDate: 2020/6/9
 */
public class NioClient {
    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);  // 默认为true

        /*
        *  @return  <tt>true</tt> if a connection was established,
         *          <tt>false</tt> if this channel is in non-blocking mode
         *          and the connection operation is in progress
        * */
        if (!socketChannel.connect(new InetSocketAddress("127.0.0.1",6666)))
            while (!socketChannel.finishConnect())
                System.out.println("请求连接超时, 不会阻塞");


        String str = "hello atguigu";
        // 将buffer的数据写入channel
        socketChannel.write(ByteBuffer.wrap(str.getBytes()));
        System.in.read();
    }
}
